import login from '../../api/login.js'
import toast from '../../utils/toast.js'
import  management from '../../api/management'
const values = {
	mobile: '',
	verification: '',
	invite_code:"",
}
const state = {
	fields: [{
			label: '手机号',
			prop: 'mobile',
			type: 'number',
			checked: true,
			labelWidth: '160',
			placeholder:'请输入手机号'
		},
		{
			label: '验证码',
			prop: 'verification',
			type: 'number',
			labelWidth: '160',
			placeholder:'请输入验证码'
		},
		{
			label: '邀请码',
			prop: 'invite_code',
			type: 'input',
			labelWidth: '160',
			placeholder:'请输入邀请码'
		},
	],
	values: JSON.parse(JSON.stringify(values)),
	rules: {
		mobile: [{
				required: true,
				message: '请输入手机号',
				trigger: 'blur,change'
			},
			{
				pattern: /^1[0-9]{10}$/g,
				// 正则检验前先将值转为字符串
				transform(value) {
					return String(value);
				},
				message: '请输入正确的手机号'
			},
		],
		verification:[
			{
					required: true,
					message: '请输入验证码',
					trigger: 'blur,change'
				},
		],
		invite_code:[
			{
			pattern: /^[0-9a-zA-Z_]{1,}$/g,
			// 正则检验前先将值转为字符串
			transform(value) {
				return String(value);
			},
			message: '邀请码只能包含数字和英文'	
			}
		]
	}
}
const mutations = {


}
const actions = {
	IDENTIFYING_CODE({state},data){
	
		return  login.sendPost(data)
		 },
   VERIFICATION_LOGIN({state}){
	  login.obligorLogin(state.values).then(res=>{
		  if(res){
			
			uni.setStorageSync('token', res.data.userinfo.token);
			uni.setStorageSync('userinfo', res.data.userinfo); 
			
			management.userInfoPost().then(res=>{
				if(res){
					uni.hideLoading();
                  if(!res.data){
					uni.setStorageSync('flag',false); 
					toast.successTab('登录成功','/pages/management/index')
				  }else{
					  uni.setStorageSync('check_status', res.data.check_status);
					  uni.setStorageSync('app_index', res.data.app_index); 
					if(res.data.check_status==0){
						toast.successTo('登录成功','/pages/management/waitFor')
						
				   }else if(res.data.check_status==1){
					toast.successTab('登录成功','/pages/management/index')
					uni.setStorageSync('flag',true); 
				   }else if(res.data.check_status==2){
					toast.successTab('登录成功','/pages/management/index')
					uni.setStorageSync('flag',false); 
				   }else if(res.data.check_status==3){
					toast.successTo('登录成功','/pages/management/customer')
					uni.setStorageSync('flag',false); 
				   }
				   else if(res.data.app_index==1){
					toast.successTo('登录成功','/pages/management/welcome')
				   }else{
					toast.successTab('登录成功','/pages/management/index')
					uni.setStorageSync('flag',false); 
				   }
					
				  }
				 
				}
				
			})
		  }
	  })   
   }
}
export default {
	 namespaced: true,
	state,
	mutations,
	actions
}
